package com.zhkj.txg.module.login.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.login.entity.SplashAdResponse
import com.zhkj.txg.module.login.model.LoginManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

class SplashViewModel(application: Application) : BaseViewModel(application) {
    private val manager = LoginManager()

    val splashAdLiveData = MutableLiveData<HttpResponse<SplashAdResponse>>()
    val countDownLiveData = MutableLiveData<Long>()

    fun splash() {
        val splashAd = manager.splashAd(AccountManager.getToken(), ViewModelSubscribeListener(splashAdLiveData))
        dispose.add(splashAd)
    }

    /**
     * 倒计时3秒
     */
    fun startCountDown(time: Long = 3) {
        val subscribe = Observable.interval(1, TimeUnit.SECONDS)
            .take(time + 1).map { time - it }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ countDownLiveData.value = it }, { it.printStackTrace() })
        dispose.add(subscribe)
    }
}